package com.bdqn.petmalldemo.utils;

import com.bdqn.petmalldemo.common.ResultCodeEnum;
import com.bdqn.petmalldemo.common.ReturnResult;
import com.bdqn.petmalldemo.common.TokenCache;
import org.springframework.util.StringUtils;

/**
 * @Author:彭俊
 * @description:公共的工具类
 * @date:2020/3/51700
 */
public class CommonUtils  {
    /**
     * 校验用户是否登录
     * @param token
     */
    public static ReturnResult checkUserLogin(String token) {
        //校验token是否有效
        if (StringUtils.isEmpty(token)) {
            return ReturnResult.error().code(ResultCodeEnum.TOKEN_NULL.getCode())
                    .msg(ResultCodeEnum.TOKEN_NULL.getMsg());
        }
        String jsonUser = TokenCache.get(token);
        if (StringUtils.isEmpty(jsonUser)) {
            return ReturnResult.error().code(ResultCodeEnum.TOKEN_INVALID.getCode())
                    .msg(ResultCodeEnum.TOKEN_INVALID.getMsg());
        }
        return ReturnResult.ok();
    }
}
